<?php

namespace App\Services;

use App\Models\ElectricAccountingRecords;
use App\Models\ElectricPaymentOrder;
use App\Models\ElectricPlatformAgent;
use App\Models\Users;

/**
 * 企业付款到零钱操作
 * Class TransferBankService
 * @package App\Services
 */
class TransferBankService extends BaseService
{

    /**
     * 2021/3/22--9:10
     * @effect:根据分账模式，处理分帐方法
     * @param $data
     * @return array|null
     * @author:苏冬冬
     * @QQ:1299273928
     * @method:transferBankHandle
     */
    public static function transferBankHandle($data): ?array
    {
        if (!is_array($data)) {
            $data = json_decode($data, 1);
        }
        $order_fields = 'id,fatherId,orderNo,pay_type,pay_no,split_money,paymentMoney,after_handling,after_split,handlingFee,user_id';
        $order = ElectricPaymentOrder::selectRaw($order_fields)
            ->where(['orderNo' => $data['orderNo']])
            ->findOrEmpty()->toArray();//根据订单号查询订单

        if ($order['split_money'] > 0) {
            $agent_fields = 'wechat_account_type,wechat_account,sub_mch_id,id,alipay_account,alipay_account_type,account_bank,bank_user_name,bank_card_number,is_examine';
            $agent = ElectricPlatformAgent::selectRaw($agent_fields)
                ->fetchSql(false)
                ->find($order['fatherId']);
            if (!$agent) {
                return ["code" => 400, "msg" => "未配置服务商"];
            }
            switch ($data['pay_type']) {
                case 1://微信支付付款到银行卡
                    $result = self::WechatTransferDo(compact('order', 'agent'));
                    $b = ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS');
                    if ($b) {
                        $bool = self::afterTrans($order, $agent);
                        $order_fields = "分账成功 分账金额：{$order['split_money']}；提现手续费：{$order['handlingFee']}；分账结余：{$order['after_split']}；订单号：{$order['orderNo']}";
                        return ['code' => 200, 'msg' => $order_fields];
                    }

                    $order_fields = "分账错误 错误代码:{$result['err_code']}；错误描述：{$result['err_code_des']}";
                    return ['code' => 400, 'msg' => $order_fields];
                    break;
                case 2://支付宝付款到银行卡
//                    $result = self::AlipayTransferDo(compact('order', 'agent'));
//                    $b = $result['code'] && $result['code'] === '10000';
//                    if ($b) {
//                        $bool = self::afterTrans($order, $agent);
//                        $order_fields = "分账成功 分账金额：{$order['split_money']}；提现手续费：{$order['handlingFee']}；分账结余：{$order['after_split']}；订单号：{$order['orderNo']}";
//                        return ['code' => 200, 'msg' => $order_fields];
//                    }
//
//                    $order_fields = "分账失败 状态码：{$result['sub_code']}；错误描述：{$result['msg']}；订单号：{$order['orderNo']}";
//                    return ['code' => 400, 'msg' => $order_fields];
                    break;
            }
        } else {
            return ['code' => 400, 'msg' => '分账金额不正确'];
        }
    }

    /**
     * 2021/3/22--9:43
     * @effect:微信付款到银行卡操作
     * @param $p
     * @return array|null
     * @author:苏冬冬
     * @QQ:1299273928
     * @method:WechatTransferDo
     */
    public static function WechatTransferDo($p): ?array
    {
        $one_order = $p['order'];//订单
        $agent = $p['agent'];
        extract($agent, null);
        /**
         * @var $bank_card_number |银行卡号
         * @var $bank_user_name |收款人姓名
         * @var  $account_bank |开户行
         */
        $result = app('wechat_api')->transfer->toBankCard([
            'partner_trade_no' => $one_order['orderNo'],
            'enc_bank_no' => $bank_card_number, // 银行卡号
            'enc_true_name' => $bank_user_name,   // 银行卡对应的用户真实姓名
            'bank_code' => $account_bank, // 银行编号
            'amount' => $one_order['split_money'] * 100,  // 单位：分
            'desc' => '付款给' . $bank_user_name,
        ]);
        if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') {
            return $result;
        }

        return $result;
    }

    /**
     * 2021/3/22--9:42
     * @effect:转账后操作
     * @param array $order
     * @param $agent
     * @return mixed
     * @author:苏冬冬
     * @QQ:1299273928
     * @method:afterTrans
     */
    public static function afterTrans(array $order, $agent)
    {
        extract($order, null);
        /**
         * @var $user_id |业主id
         * @var $fatherId |代理商id
         * @var $orderNo |订单号
         * @var $is_examine |是否审核
         */
        ElectricPaymentOrder::where(['orderNo' => $orderNo])->update(['is_split' => 2]);
        $arr1 = ['orderNo' => $order['orderNo'], 'from_user' => $user_id, 'agent_id' => $fatherId];
        return ElectricAccountingRecords::where($arr1)->update(['status' => 2]);
    }
}
